package com.lnsoft.cac.server.handle.webservice.MsgHandle;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class UploadCACData {
    private Element rootNode;
    private Document doc;
    private Element monitorDataNode;
    private Element dataNode;
    private Element attrsNode;

    public UploadCACData() {
        this.rootNode = new Element("request");
        this.doc = new Document(this.rootNode);
        this.monitorDataNode = new Element("monitordata");
        this.rootNode.addContent(this.monitorDataNode);
        this.dataNode = new Element("datanode");
        this.rootNode.addContent(this.dataNode);
        this.attrsNode = new Element("attrs");
        this.dataNode.addContent(this.attrsNode);
    }

    public void setMonDataNodeAttr(String strCACId, String strDatanodeNum) {
        this.monitorDataNode.setAttribute("cacid", strCACId);
        this.monitorDataNode.setAttribute("datanodenum", strDatanodeNum);
    }

    public void setDataNodeAttr(String strDeviceId, String strMeasurePointNo, String strType, String cmdId, String strTimesTamp) {
        this.dataNode.setAttribute("deviceid", strDeviceId);
        this.dataNode.setAttribute("measurepointno", strMeasurePointNo);
        this.dataNode.setAttribute("type", strType);
        if (cmdId != null) {
            this.dataNode.setAttribute("commandid", cmdId);
        }
        this.dataNode.setAttribute("timestamp", strTimesTamp);
    }

    public void addAttrNode(String strName, String strValue) {
        Element attrElement = new Element("attr");
//        attrElement.setAttribute("desc", strName);
        attrElement.setAttribute("name", strName);
        attrElement.setAttribute("value", strValue);

        //添加到父节点
        this.attrsNode.addContent(attrElement);
    }

    public String getMsg() {
        XMLOutputter XMLOut = new XMLOutputter(Format.getPrettyFormat());
        return XMLOut.outputString(this.doc);
    }
}
